package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"house/chain/contract_client"
)

type ContractController struct {
	beego.Controller
}

// GetAll 获取所有合同数据
func (c *ContractController) GetAllContracts() {
	result, err := contract_client.GetAllContracts()

	if err != nil {
		FailResponse(c.Controller, err.Error())
		return
	}

	OkResponse(c.Controller, result, "")
}

// AddContract 添加合同
func (c *ContractController) AddContract() {

	tenant := c.GetString("tenant")
	landlord := c.GetString("landlord")
	HouseAddress := c.GetString("HouseAddress")
	contracturl := c.GetString("contracturl")

	result, err := contract_client.CreateAsset(tenant, landlord, HouseAddress, contracturl)
	if err != nil {
		FailResponse(c.Controller, err.Error())
		return
	}

	OkResponse(c.Controller, result, "")
}

// GetContractById 根据ID获取合同信息
func (c *ContractController) GetContractById() {
	houseId := c.GetString("houseId")

	result, err := contract_client.ReadAssetByID(houseId)
	if err != nil {
		FailResponse(c.Controller, err.Error())
		return
	}

	OkResponse(c.Controller, result, "")
}
